package com.road.crawler.meizitu.crawler;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.geccocrawler.gecco.pipeline.Pipeline;
import com.road.crawler.meizitu.mapper.PictureMapper;
import com.road.crawler.meizitu.model.Blog;
import com.road.crawler.meizitu.service.PictureInfoService;

/**
 * @Description :
 * @Author :WeiHui.Zhang
 * @Data : 2016/3/30 11:06
 * @Version:1.0.0
 */
@Service
public class NextPagePipeline implements Pipeline<SecondPage> {

	@Resource(name="pictureInfoServiceImpl")
	private PictureInfoService pictureInfoService;

	@Autowired
	private PictureMapper pictureMapper;
	@Override
	public void process(SecondPage bean) {
   		System.out.println(bean.toString());
   		Blog blog = new Blog();
   		blog.setTitle(bean.getTitle());
   		blog.setContent(bean.getContent());
   		blog.setReleaseDate(new Date());
   		if(StringUtils.isNotBlank(bean.getContent())){
   			if(bean.getContent().length()>200){
   				blog.setSummary(bean.getContent().substring(0,200));
   			}else{
   				blog.setSummary(bean.getContent());
   			}
   			
   		}
   		
   		blog.setReplyHit(0);
   		blog.setClickHit(RandomUtils.nextInt(160, 2000));
   		List<String> keywords = bean.getKeywords();
   		String keyWordStr = "";
   		if(!CollectionUtils.isEmpty(keywords)){
   			for(String str:keywords){
   	   			keyWordStr+=str+" ";
   	   		}
   			if(StringUtils.isNotBlank(keyWordStr)){
   				keyWordStr = keyWordStr.substring(0, keyWordStr.length()-1);
   			}
   	   		blog.setKeyWord(keyWordStr);
   		}
   		
   		if(blog!=null){
   			pictureMapper.insertBlog(blog);
   		}
		// 保存当前页爬取的图片信息
//		List<Picture> pictures = bean.getPictures();
//		for (Picture picture : pictures) {
//			PictureInfo pictureInfo = new PictureInfo();
//			pictureInfo.setAlt(picture.getAlt());
//			pictureInfo.setHref(picture.getHref());
//			pictureInfo.setThumbnail(picture.getThumbnail());
//			pictureInfo.setCreateDate(new Date());
//			pictureInfoService.save(pictureInfo);
//			//继续抓取详情页
//			HttpRequest sub = bean.getRequest().subRequest(picture.getHref());
//			sub.addParameter("picInfoId", ""+pictureInfo.getId());
//			SchedulerContext.into(sub);
//		}
		//继续抓取下一页构造下一页
//		int currPage = bean.getCurrPage();
//		int totalPage = getTota lPage(bean.getLastPageUrl());
//		if (currPage < totalPage) {
//			int nextPage = currPage + 1;
//			String nextUrl = "http://www.meizitu.com/tag/"+bean.getTagCode()+"_"+bean.getTagId()+"_"+nextPage+".html";
//			HttpRequest request = bean.getRequest();
//			SchedulerContext.into(request.subRequest(nextUrl));
//		}
	}
	
	private int getTotalPage(String lastPageUrl) {
		if(StringUtils.isEmpty(lastPageUrl)) {
			return 1;
		}
		//http://www.meizitu.com/tag/ suxiong_17_ 9 .html
		lastPageUrl = StringUtils.substringAfterLast(lastPageUrl, "/");
		lastPageUrl = StringUtils.substringBefore(lastPageUrl, ".");
		lastPageUrl = StringUtils.substringAfterLast(lastPageUrl, "_");
		return NumberUtils.toInt(lastPageUrl, 1);
	}
}
